package com.example.eos.service;

import com.alibaba.fastjson.JSON;
import com.example.eos.bean.Chain;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpService {

    //GET JSON格式
    public static String DoGet(String url){
        //设置连接配置
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000)
                .setSocketTimeout(5000).setConnectionRequestTimeout(5000).build();
        //创建HttpClient实例
        HttpClient httpClient = HttpClientBuilder.create().build();
        //创建GET的连接方式实例
        HttpGet httpGet = new HttpGet(url);
        //配置请求头
        httpGet.addHeader("content-type", "application/json");
        httpGet.addHeader("Accept", "application/json");
        httpGet.setConfig(requestConfig);
        try{
            //接收响应信息
            HttpResponse httpResponse = httpClient.execute(httpGet);
            //如果响应信息正确返回响应体信息
            if(httpResponse.getStatusLine().getStatusCode() == 200){
                return EntityUtils.toString(httpResponse.getEntity());
            }else {
                return "{\"message\":\"请求失败\"}";
            }
        }catch (IOException e){
            return "{\"message\":\"IOException异常\"}";
        }finally {
            //释放连接
            httpGet.releaseConnection();
        }
    }

    //POST JSON格式 chain
    public static String DoPost(String url, Chain chain){
        //设置连接配置
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000)
                .setSocketTimeout(5000).setConnectionRequestTimeout(5000).build();
        //创建HttpClient实例
        HttpClient httpClient = HttpClientBuilder.create().build();
        //创建POST的连接方式实例
        HttpPost httpPost = new HttpPost(url);
        //配置请求头
        httpPost.addHeader("content-type", "application/json");
        httpPost.addHeader("Accept", "application/json");
        httpPost.setConfig(requestConfig);
        try{
            //添加请求题信息
            String jsonString = JSON.toJSONString(chain);
            System.out.println(jsonString);
            StringEntity entity = new StringEntity(jsonString);
            httpPost.setEntity(entity);
            //接收响应信息
            HttpResponse httpResponse = httpClient.execute(httpPost);
            //如果响应信息正确返回响应体信息
            if(httpResponse.getStatusLine().getStatusCode() == 200){
                return EntityUtils.toString(httpResponse.getEntity());
            }else {
                return "{\"message\":\"请求失败\"}";
            }
        }catch (IOException e){
            return "{\"message\":\"IOException异常\"}";
        }finally {
            //释放连接
            httpPost.releaseConnection();
        }
    }

    //POST 默认格式
    public static String DoPost(String url, String string){
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000)
                .setSocketTimeout(5000).setConnectionRequestTimeout(5000).build();
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(requestConfig);
        try{
            StringEntity entity = new StringEntity(string);
            httpPost.setEntity(entity);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            if(httpResponse.getStatusLine().getStatusCode() == 200){
                return EntityUtils.toString(httpResponse.getEntity());
            }else {
                return "{\"message\":\"请求失败\"}";
            }
        }catch (IOException e){
            return "{\"message\":\"IOException异常\"}";
        }finally {
            //释放连接
            httpPost.releaseConnection();
        }
    }

}
